package com.wonderful.mall.seckill.scheduled;

import com.wonderful.mall.seckill.service.SecKillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SecKillScheduled {
    @Resource
    SecKillService secKillService;
    @Resource
    RedissonClient redissonClient;
    private final String upload_lock = "secKill:upload:lock";

    @Scheduled(cron = "0 0/1 0/1 1/1 * ?")
    public void uploadSecKillLatest3Days() {
        log.info("上架秒杀商品信息");
        RLock lock = redissonClient.getLock(upload_lock);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            secKillService.uploadSecKillLatest3Days();
        } finally {
            lock.unlock();
        }
    }
}
